﻿/**
* QSmart Andon
*
* 2012-10-10
* 
* steven.li
*
* Copyright(c) 2012 All Rights Reserved. Taiyou Tech(GUANGZHOU) Co.,Ltd.  (广州太友科技) [ sales@gztaiyou.com]. 
*
*/
using System;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.SessionState;
using FX.CC.Andon.Business;
using FX.CC.Andon.Business.SystemManage;
using FX.CC.Andon.Entity.SystemManage;
using FX.CC.Andon.Utility.Common;

namespace FX.CC.Andon.WebSite.Service
{
    /// <summary>
    /// PublicHandler 的摘要说明
    /// </summary>
    public class PublicHandler : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                context.Response.ContentType = "text/plain";
                var action = DtRequest.GetQueryString("action");
                if (string.IsNullOrEmpty(action)) return;
                var method = this.GetType().GetMethod(action);
                if (method == null) return;
                object[] parameters = { context };
                method.Invoke(this, parameters);
            }
            catch (Exception ex)
            {
                EventsLog.WriteLog("ErrorInfo" + ex.Message);
            }
        }


        public void Login(HttpContext context)
        {
            var loginCode = context.Request["LoginCode"];
            var loginPwd = context.Request["LoginPwd"];
            var result = new UserBusiness().Login(loginCode, loginPwd);
            context.Response.Write(result);
            context.Response.End();
        }

        public void UserLogin(HttpContext context)
        {
            var uname = context.Request["paramName"];
            var pwd = context.Request["paramPwd"];
            if (string.IsNullOrEmpty(uname) || string.IsNullOrEmpty(pwd))
                context.Response.Write("1|" + SysParam.ErrorPageTip("1"));
            //if (RequestCheck.CheckKeyWord(uname) || RequestCheck.CheckKeyWord(pwd))
            //    context.Response.Write("3|" + SysParam.ErrorPageTip("3"));
            //else
            {
                var qcmodel = new UserQcEntity
                {
                    LoginCode = uname
                };
                var result = new UserBusiness().UserLogin(qcmodel);
                if (result.LoginPwd == SysParam.EncryptAdmin(pwd))
                {
                    //账号状态：0:已冻结，1：未冻结,2:未锁定，3:已锁定
                    switch (result.RecStatus)
                    {
                        case 0:
                            context.Response.Write("20|" + SysParam.ErrorPageTip("11"));
                            break;
                        case 3:
                            context.Response.Write("20|" + SysParam.ErrorPageTip("12"));
                            break;
                        default:
                            {
                                var tempRoleId = result.RoleID.ToString();
                                if (string.IsNullOrEmpty(tempRoleId))
                                    context.Response.Write("14|" + SysParam.ErrorPageTip("14"));
                                else
                                {
                                    context.Response.Write("10|" + SysParam.ErrorPageTip("10"));
                                    SysContext.CurrentLoginCode = result.LoginCode;
                                    SysContext.CurrentFullName = result.FullName;
                                    SysContext.CurrentRoleId = result.RoleID.ToString();
                                }
                            }
                            break;
                    }
                }
                else
                {
                    context.Response.Write("9|" + SysParam.ErrorPageTip("9"));
                }
            }
        }



        public void GetDeptList(HttpContext context)
        {
            var bll = new DeptBusiness();
            var json = bll.GetDeptList();
            context.Response.Write(json);
            context.Response.End();
        }

        public void GetRoleList(HttpContext context)
        {
            var bll = new RoleBusiness();
            var json = bll.GetRoleList();
            context.Response.Write(json);
            context.Response.End();
        }

        public void LoadSysMenuList(HttpContext context)
        {
            var loginCode = SysContext.CurrentLoginCode;
            if (string.IsNullOrEmpty(loginCode)) return;
            var json = new SysMenuBusiness().LoadSysMenuList(loginCode);
            context.Response.Write(json);
            context.Response.End();
        }

        public void CheckedCode(HttpContext context)
        {
            var tableName = context.Request["tableName"];
            var fieldName = context.Request["fieldName"];
            var code = context.Request["Code"];
            var json = new CommonBusiness().CheckedCode(tableName, fieldName, code);
            context.Response.Write(json);
            context.Response.End();
        }

        public void CheckedPwd(HttpContext context)
        {
            var loginCode = context.Request["LoginCode"];
            var pwd = context.Request["Pwd"];
            var json = new CommonBusiness().CheckedPwd(loginCode, pwd);
            context.Response.Write(json);
            context.Response.End();
        }

        public void CheckedCodes(HttpContext context)
        {
            var tableName = context.Request["tableName"];
            var fieldName1 = context.Request["fieldName1"];
            var fieldName2 = context.Request["fieldName2"];
            var code1 = context.Request["Code1"];
            var code2 = context.Request["Code2"];
            var json = new CommonBusiness().CheckedCodes(tableName, fieldName1, fieldName2, code1, code2);
            context.Response.Write(json);
            context.Response.End();
        }

        public void Freeze(HttpContext context)
        {
            var table = context.Request["table"];
            var field = context.Request["field"];
            var value = context.Request["value"];
            var json = new CommonBusiness().Freeze(table, field, value);
            context.Response.Write(json);
            context.Response.End();
        }
        public void Unfrozen(HttpContext context)
        {
            var table = context.Request["table"];
            var field = context.Request["field"];
            var value = context.Request["value"];
            var json = new CommonBusiness().Unfrozen(table, field, value);
            context.Response.Write(json);
            context.Response.End();
        }
        public void GetPinyin(HttpContext context)
        {
            var name = context.Request["Key"];
            if (string.IsNullOrEmpty(name))
            {
                return;
            }
            var json = new CommonBusiness().GetPinyin(name);
            context.Response.Write(json);
            context.Response.End();
        }

        public void GetTotalTime(HttpContext context)
        {
            var obj = Convert.ToInt32(ConfigurationManager.AppSettings["Hour"]) * 60;
            context.Response.Write(obj);
            context.Response.End();
        }

        public void DownloadExcleTemplate(HttpContext context)
        {
            string filePath = context.Request.PhysicalApplicationPath + "ExcleTemplate\\设备Excel导入模板.xls";
            if (File.Exists(filePath))
            {
                var file = new FileInfo(filePath);
                context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码
                context.Response.AddHeader("Content-Disposition", "attachment; filename=" + context.Server.UrlEncode(file.Name)); //解决中文文件名乱码    
                context.Response.AddHeader("Content-length", file.Length.ToString());
                context.Response.ContentType = "appliction/octet-stream";
                context.Response.WriteFile(file.FullName);
                context.Response.Flush();
                context.Response.Close();
                //context.Response.End();

            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}